package com.ruoyi.iot.runner;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.iot.constant.AttendanceMachineModel;
import com.ruoyi.iot.domain.IotAttendanceMachine;
import com.ruoyi.iot.domain.mqtt.DepthMqttMessage;
import com.ruoyi.iot.domain.mqtt.MqttMessage;
import com.ruoyi.iot.domain.mqtt.WillMqttMessage;
import com.ruoyi.iot.service.IDepthMqttMessageService;
import com.ruoyi.iot.service.IIotAttendanceMachineService;
import com.ruoyi.iot.service.impl.IYFHandleMqttMessageServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.iot.mqtt.spring.client.MqttClientTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * mqtt配置
 * @Author: 陈江灿
 * @CreateTime: 2025-02-14
 */
@Component
@Slf4j
public class MqttClientSubscribeListener implements ApplicationRunner {

    @Autowired
    private MqttClientTemplate client;

    @Autowired
    private IIotAttendanceMachineService iotAttendanceMachineService;

    @Autowired
    private IYFHandleMqttMessageServiceImpl yfMqttMessageService;

    @Autowired
    private IDepthMqttMessageService depthMqttMessageService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<IotAttendanceMachine> machines = iotAttendanceMachineService.selectIotAttendanceMachineList(null);
        for(IotAttendanceMachine attendanceMachine : machines){
            // 宇泛人脸机
            if(AttendanceMachineModel.ATTENDANCE_MACHINE_MODEL_0.equals(attendanceMachine.getModel())){
                client.subQos0("device/Uface/" + attendanceMachine.getSn(), (context, topic, message, payload) -> {
                    log.info(topic + '\t' + new String(payload, StandardCharsets.UTF_8));
                });
                client.subQos0("device/Uface/" + attendanceMachine.getSn() + "/Upstream", (context, topic, message, payload) -> {
                    String data = new String(payload, StandardCharsets.UTF_8);
                    MqttMessage mqttMessage = JSONObject.parseObject(data, MqttMessage.class);
                    yfMqttMessageService.handle(mqttMessage, topic);
                });
                client.subQos0("device/Uface/" + attendanceMachine.getSn() + "/Downstream", (context, topic, message, payload) -> {
                    log.info(topic + '\t' + new String(payload, StandardCharsets.UTF_8));
                });
                client.subQos0("device/Uface/" + attendanceMachine.getSn() + "/will", (context, topic, message, payload) -> {
                    String data = new String(payload, StandardCharsets.UTF_8);
                    WillMqttMessage willMqttMessage = JSONObject.parseObject(data, WillMqttMessage.class);
                    yfMqttMessageService.handleWill(willMqttMessage, topic);
                });
            }
            // 图深人脸机
            if(AttendanceMachineModel.ATTENDANCE_MACHINE_MODEL_1.equals(attendanceMachine.getModel())){
                String upstreamTopic = "device/tushen/" + attendanceMachine.getSn() + "/Upstream";
                String downstreamTopic = "device/tushen/" + attendanceMachine.getSn() + "/Downstream";
                upstreamTopic = upstreamTopic.replaceAll("[^a-zA-Z0-9_/]", "");
                downstreamTopic = upstreamTopic.replaceAll("[^a-zA-Z0-9_/]", "");
                client.subQos0(upstreamTopic, (context, topic, message, payload) -> {
                    String data = new String(payload, StandardCharsets.UTF_8);
                    DepthMqttMessage mqttMessage = JSONObject.parseObject(data, DepthMqttMessage.class);
                    depthMqttMessageService.handle(mqttMessage, topic);
                });
                client.subQos0(downstreamTopic, (context, topic, message, payload) -> {
                    log.info(topic + '\t' + new String(payload, StandardCharsets.UTF_8));
                });
            }
        }
    }


}
